layui.use(['layer', 'form', 'table', 'upload', 'dropdown', "laydate"], function () {
    var layer = layui.layer
        , form = layui.form
        , $ = layui.$
        , table = layui.table
        , upload = layui.upload
        , dropdown = layui.dropdown;
    table.render({
        elem: '#Rate'//表格的ID
        , url: './selectExchangeRate'//表格的数据源 controller里的方法
        , toolbar: '#userToolBar'//通过工具条的ID来区分
        ,cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,height:'full-180'
        , page: true//开启分页
        , limit: 5//默认进去每页显示5条
        , limits: [5, 10, 15]//自己设置下拉列表5,10,15，行
        , methods: "post"// 请求方式
        , cols: [[
            {type: 'checkbox', field: 'left'},
            //field 你的实体类属性名字    title 标题     sort: true 开启排序   templet 给列取别名
            {field: 'exchangeRateId', align:'center', title: '交易品种费率id' ,hide:true}
            , {
                field: 'exchangeRateName', align:'center', title: '交易所名称', templet: function (provider) {
                    return provider.exchangeRateName == 1 ? '上交所' : '深交所';
                }
            }, {
                field: 'exchangeType', align:'center', title: '交易品种', templet: function (provider) {
                    return provider.exchangeType > 0 && provider.exchangeType == 1 ? '股票' : '债券';
                }
            }, {field: 'stampDuty',  title: '印花税',align:'center'}
            , {field: 'transferFee',  title: '过户费',align:'center'}
            , {field: 'handlingFee',  title: '经手费',align:'center'}
            , {field: 'collectionFee',  title: '征管费',align:'center'}
            , {field: '', title: '操作', align:'center', toolbar: '#toolEventDemo',fixed: 'right'}
        ]],
        //返回参数取名字
        request: {
            pageName: 'page',   // 页码的参数名称，默认：page
            limitName: 'row',  // 每页数据量的参数名，默认：limit
        }
    });

    //工具栏
    table.on('toolbar(Rate)', function (obj) {
        var id = obj.config.id;
        var checkStatus = table.checkStatus(id);
        var data = checkStatus.data;
        switch (obj.event) {
            case 'search':
                Refresh()
                break;
            case 'deleteAll':
                if (data.length <= 0) {
                    layer.msg('请选择要从删除的行!', {icon: 0, time: 1000, shade: [0.5, '#000', true]});
                } else if (data.length == 1) {
                    layer.confirm('确认删除?', function () {
                        Delete(data, true)
                    })
                } else {
                    layer.confirm('确认批量删除?', function () {
                        Delete(data, true)
                    })
                }
                break;
            case 'add':
                var index = layer.open({
                    type: 1,
                    title: '添加交易所品种费率',
                    closeBtn: 1,
                    move: false,
                    content: $("#addContent"),
                    btn: [],
                    area: ['500px', '550px'],
                    end: function () {
                        $("#addform")[0].reset()
                    }
                });
                break;
                form.render();
        }
    })

    //单元行
    table.on('tool(Rate)', function (obj) { // 双击 toolDouble
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确认删除?', function () {
                Delete(data.exchangeRateId, false);
            });
        } else if (obj.event === 'edit') {
            form.val('editform', $.parseJSON(JSON.stringify(data)));
            var index = layer.open({
                type: 1,
                title: '修改交易数据',
                closeBtn: 1,
                move: false,
                area: ['400px', '500px'],
                content: $('#editContent')
            });
            form.render();
        }
    })

    function Delete(MyData, flag) {
        let Arr = [];
        if (!flag) {
            Arr.push(MyData)
        } else {
            for (var i = 0; i < MyData.length; i++) {
                Arr.push(MyData[i].exchangeRateId);
            }
        }

        $.post("./deleteExchangeRate", {myArr: JSON.stringify(Arr)}, function () {
            if (Arr.length <= 1) {
                layer.msg('删除成功!', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
            } else {
                layer.msg('批量删除成功!', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
            }
            Refresh()
        })
    }


    function Refresh() {
        var exchangeRateName = $("#exchangeRateName").val()
        var exchangeType = $("#exchangeType").val()
        table.reload('Rate', {
            method: 'post'
            , where: {		//请求带的参数，json格式
                exchangeRateName: $("#exchangeRateName").val(),
                exchangeType: $("#exchangeType").val()
            }
            , page: {		//分页参数
                curr: 1		//搜素后从第一页开始展示
            }
        });
        $("#exchangeRateName").val(exchangeRateName)
        $("#exchangeType").val(exchangeType)
    }

    //新增提交
    form.on('submit(addsubmit)', function (data) {
        //获取表格所有最新数据
        var add1 = document.getElementById("RateName_2").value;
        var add2 = document.getElementById("rateType_2").value;
        if (!add1 > 0 || add2 > 0) {
            var formData = $('#addform').serialize();
            var datas = table.cache["Rate"];
            for (var i = 0; i < datas.length; i++) {
                if (datas[i].exchangeRateName == add1 && datas[i].exchangeType == add2) {
                    layer.msg('该交易所费率类型已存在!', {icon: 0, time: 1000, shade: [0.5, '#000', true]});
                }
            }
            $.post("/addExchangeRate", formData, function (msg) {
                if (msg == "1") {
                    table.reload('Rate');
                    layer.closeAll();
                    layer.msg('添加成功', {icon: 1, time: 1000, shade: [0.5, '#000', true]})
                }
            });
        } else {
            layer.msg('请选择交易所或费率类型!', {icon: 2, time: 1000, shade: [0.5, '#000', true]});
        }
        return false;
    })


    //修改提交
    form.on('submit(editsubmit)', function (data) {
        var formData = $('#editform').serialize();
        $.post("/updateExchangeRate", formData, function (msg) {
            if (msg == "ok") {
                table.reload('Rate');
                layer.closeAll();
                layer.msg('修改成功', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
            } else if (msg == "error") {
                layer.msg('修改失败!', {icon: 2, time: 1000, shade: [0.5, '#000', true]});
            } else if (msg == "null") {
                layer.msg('请选择要修改交易所或费率类型!', {icon: 0, time: 1000, shade: [0.5, '#000', true]});
            }
        });
        return false;
    })

    $(document).ready(function () {
        $(":button[name='offSubmit']").click(function () {
            $("#addform")[0].reset()
            layer.closeAll()
        })
    })
});
//取消框
function quxiao(){
    layer.closeAll();
}

